package com.asia5b.a5mall.order.adapter

import android.content.Intent
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.model.bean.CartMultiItemEntity
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel

import com.asia5b.a5mall.middleware.utils.CurrencyPriceBuilder3
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.activity.PlaceGoodListActivity
import com.asia5b.a5mall.order.databinding.ItemOrderplaceGoodsBinding
import com.asia5b.a5mall.order.databinding.ItemOrderplaceShopBinding
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/1/6.
 * 商品
 */
class OrderPlaceGoodsAdapter(var list: ArrayList<CartMultiItemEntity>) :
        BaseMultiItemQuickAdapter<CartMultiItemEntity, BaseDataBindingHolder<*>>(list) {

    fun startPlaceGoodListActivity(list: ArrayList<AbsGoodsModel>) {
        val intent = Intent(context, PlaceGoodListActivity::class.java)
        intent.putExtra("datas", list)
        context.startActivity(intent)
    }

    init {
        addItemType(CartMultiItemEntity.CartShopItem, R.layout.item_orderplace_shop)
        addItemType(CartMultiItemEntity.Holder, R.layout.item_divider_10dp)
        addItemType(CartMultiItemEntity.CartGoodsItem, R.layout.item_orderplace_goods)
    }

    override fun convert(helper: BaseDataBindingHolder<*>, item: CartMultiItemEntity) {
        helper.setIsRecyclable(false)
//        val viewDataBindingTag = helper.itemView.getTag(R.id.BaseQuickAdapter_databinding_support);

        when (item.itemType) {
            CartMultiItemEntity.CartShopItem -> {
                setCartShopItemView(helper, item as AbsShopModel, helper.dataBinding as ItemOrderplaceShopBinding)
            }
            CartMultiItemEntity.Holder -> {
            }
            CartMultiItemEntity.CartGoodsItem -> {
                setCartGoodsItemView(helper, item as AbsGoodsModel, helper.dataBinding as ItemOrderplaceGoodsBinding)
            }
        }
    }

    /**
     * 购物车店铺Item适配器
     */

    fun setCartShopItemView(helper: BaseViewHolder, shopItemData: AbsShopModel, binding: ItemOrderplaceShopBinding) {
        binding.data = shopItemData

        helper.itemView.setOnClickListener {
            startPlaceGoodListActivity(shopItemData.absShopGoodsList!!)
        }
    }

    /**
     * 购物车商品Item适配器
     */
    fun setCartGoodsItemView(helper: BaseViewHolder, cartGoodsItemViewModel: AbsGoodsModel, binding: ItemOrderplaceGoodsBinding) {
        binding.data = cartGoodsItemViewModel

        //价格
        val tv_shopcar_price = helper.getView<TextView>(R.id.tv_orderplace_price)
        CurrencyPriceBuilder3().run {
            textView = tv_shopcar_price
            price = cartGoodsItemViewModel.absGoodsPrice
            priceList = cartGoodsItemViewModel.absGoodsPriceList
            build()
        }

        //重量
        val tv_shopcar_weight = helper.getView<TextView>(R.id.tv_orderplace_weight)
        tv_shopcar_weight.text = String.format(ResString.get(R.string.重量_数), cartGoodsItemViewModel.absGoodsWeight)

        //数量
        val tv_orderplace_num = helper.getView<TextView>(R.id.tv_orderplace_num)
        tv_orderplace_num.text = String.format("X%s", cartGoodsItemViewModel.absGoodsCurrentBuyNum)

        helper.itemView.setOnClickListener {
            for (cartMultiItemEntity in list) {
                if (cartMultiItemEntity is AbsShopModel) {
                    if (cartMultiItemEntity.absShopId == cartGoodsItemViewModel.absGoodsShopId) {
                        startPlaceGoodListActivity(cartMultiItemEntity.absShopGoodsList!!)
                        break
                    }
                }
            }
        }

    }

}